/**    
 * @{#} PBEEncoderIDEAImpl.java Create on Mar 18, 2012 10:49:45 AM    
 *    
 * Copyright (c) 2012 by 悠忧虫.    
 *    
 * @author   
 * @version 1.0    
 */
package tk.sunlatus.project.security.encode.impl;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import org.bouncycastle.util.encoders.Hex;
import org.springframework.util.Assert;

import tk.sunlatus.project.common.constants.SunlatusConstants;
import tk.sunlatus.project.common.constants.SunlatusConstants.EncodeConstant;
import tk.sunlatus.project.security.encode.PBEEncoder;

/**
 * @{# PBEEncoderIDEAImpl.java Create on Mar 18, 2012 10:49:45 AM
 * 
 *     类功能说明:
 * 
 *     <p>
 *     Copyright: Copyright(c) 2012
 *     </p>
 * @Version 1.0
 * @Author
 */
public class PBEEncoderIDEAImpl implements PBEEncoder {

    private Key toKey(String password) {
        Assert.notNull("can not encrypt,cause: the password is null", password);
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
        Key key = null;
        try {
            SecretKeyFactory keyFactory = SecretKeyFactory
                .getInstance(SunlatusConstants.EncodeConstant.ALGORITHM_PBE_WITH_IDEA);
            key = keyFactory.generateSecret(keySpec);
        } catch (NoSuchAlgorithmException e) {
            // TODO log
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            // TODO log
            e.printStackTrace();
        }
        return key;
    }

    /**
     * {@inheritDoc}
     */
    public String encrypt(String data, String password, String salt) {

        PBEParameterSpec paramSpec = new PBEParameterSpec(Hex.decode(salt.getBytes(EncodeConstant.CHARSET)),
            EncodeConstant.encryptTimes);
        String encodedData = null;
        try {
            Cipher cipher = Cipher.getInstance(EncodeConstant.ALGORITHM_PBE_WITH_IDEA);
            Key key = toKey(password);
            if (key == null) {
                // TODO logger.log("get key faild.");
                return null;
            }
            cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            byte[] encodeBytes = cipher.doFinal(data.getBytes(EncodeConstant.CHARSET));
            encodedData = new String(Hex.encode(encodeBytes), EncodeConstant.CHARSET);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return encodedData;
    }
    
    
    
}
